<template>
  <div class="setting">
    <el-card>
      <el-tabs>
        <el-tab-pane label="用户管理" lazy>
          <el-button
            type="primary"
            style="margin-top: 20px"
            @click="dialogBtn"
          >新增角色</el-button>
          <el-table :data="list">
            <el-table-column label="序号">
              <template v-slot="{ $index }">
                <div>
                  {{ (page.page - 1) * page.pagesize + $index + 1 }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="角色名" width="180" prop="name" />
            <el-table-column label="描述" prop="description" />
            <el-table-column label="操作">
              <template v-slot="{ row }">
                <div>
                  <el-button type="text">权限分配</el-button>
                  <el-button type="text" @click="edit(row)">修改</el-button>
                  <el-button type="text" @click="del(row.id)">删除</el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <div class="page">
            <el-pagination
              :current-page="page.page"
              :page-sizes="[1, 5, 100, 200]"
              :page-size="page.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="page.total"
              @size-change="sizeChange"
              @current-change="pageChange"
            />
          </div>
        </el-tab-pane>
        <el-tab-pane label="公司信息" lazy> <info /></el-tab-pane>
      </el-tabs>
    </el-card>
    <add ref="add" :show.sync="show" @getData="getData" />
  </div>
</template>

<script>
import info from './components/info.vue'
import { sysRole, sysRoleDel } from '@/api/setter'
import add from './components/add.vue'
export default {
  components: { info, add },
  data() {
    return {
      show: false,
      list: [{}],
      page: {
        total: 100,
        page: 1,
        pagesize: 5
      }
    }
  },
  // 初始化渲染数据
  created() {
    this.getData()
  },
  methods: {
    sizeChange(size) {
      this.page.pagesize = size
      this.page.page = 1
      this.getData()
    },
    pageChange(page) {
      this.page.page = page
      this.getData()
    },
    // 获取数据
    async getData() {
      const res = await sysRole(this.page)
      // console.log(res)
      this.list = res.data.rows
      this.page.total = res.data.total
    },
    // 新增
    dialogBtn() {
      this.$refs.add.mode = 'add'
      this.show = true
    },
    // 删除
    del(v) {
      this.$confirm('是否删除', '温馨提示')
        .then(async() => {
          await sysRoleDel(v)
          this.$message.success('删除成功')
          this.page.page = 1
          this.getData()
        })
        .catch(() => {})
    },
    // 编辑
    edit(v) {
      this.show = true
      this.$refs.add.mode = 'edit'
      this.$refs.add.form = JSON.parse(JSON.stringify(v))
    }
  }
}
</script>

<style lang="scss" scoped>
.setting {
  padding: 15px;
  .page {
    text-align: right;
    margin-top: 20px;
  }
}
</style>
